<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图点击切换</title>
    <style>
        .box{
            position: relative;
            width: 600px;
            height: 400px;
            margin: 50px auto;
            border:20px solid white;
            box-sizing: border-box;
            box-shadow: 0 0 5px black;
            overflow:hidden;
        }
        .img{
            width:100%;
        }
        .bottom{
            position: absolute;
            bottom:0;
            width: 100%;
            padding: 20px;
            box-sizing: border-box;
            color:white;
            background-color: rgba(0,0,0,0.5);
        }
        .title{
            margin: 0;
        }
        .dots{
            position: absolute;
            bottom: 10px;
            left:100px;
            transform: translate(-50%);
            display: flex;
            gap:10px;
        }
        .dot{
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color:grey;
            cursor:pointer;
        }
        .dot.active{
            background-color: #fff;
        }
        .btn {
            position: absolute;
            top: 89%;
            transform: translateY(-50%);
            width: 30px;
            height: 30px;
            background-color: rgba(0,0,0,0.5);
            color: #fff;
            border: none;
            cursor: pointer;
            font-size: 20px;
        }
        .left-btn {
            right:50px;
        }
        .right-btn {
          left:520px
        }
    </style>
</head>
<body>
    <div class="box">
        < img src="" class="img" alt="图片">
        <div class="bottom">
            <h4 class="title"></h4>
        </div>
        <div class="dots">
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
        </div>
        <button class="btn left-btn"><</button>
        <button class="btn right-btn">></button>
    </div>
    <script>
        const sliderData = [
      { url: './images/slider01.jpg', title: '对人类来说会不会太超前了？', color: 'rgb(100, 67, 68)' },
      { url: './images/slider02.jpg', title: '开启剑与雪的黑暗传说！', color: 'rgb(43, 35, 26)' },
      { url: './images/slider03.jpg', title: '真正的jo厨出现了！', color: 'rgb(36, 31, 33)' },
      { url: './images/slider04.jpg', title: '李玉刚：让世界通过B站看到东方大国文化', color: 'rgb(139, 98, 66)' },
      { url: './images/slider05.jpg', title: '快来分享你的寒假日常吧~', color: 'rgb(67, 90, 92)' },
      { url: './images/slider06.jpg', title: '哔哩哔哩小年YEAH', color: 'rgb(166, 131, 143)' },
      { url: './images/slider07.jpg', title: '一站式解决你的电脑配置问题！！！', color: 'rgb(53, 29, 25)' },
      { url: './images/slider08.jpg', title: '谁不想和小猫咪贴贴呢！', color: 'rgb(99, 72, 114)' },
    ] 
    let index = 0;
    let timer = null;

    const img = document.querySelector('.img');
    const title = document.querySelector('.title');
    const bottom = document.querySelector('.bottom');
    const dots = document.querySelectorAll('.dot');
    const leftBtn = document.querySelector('.left-btn');
    const rightBtn = document.querySelector('.right-btn');
    const box = document.querySelector('.box');

    function updateSlider() {
        const data = sliderData[index];
        img.src = data.url;
        title.textContent = data.title;
        bottom.style.backgroundColor = data.color;
        
        dots.forEach((dot, i) => {
            if (i === index) {
                dot.classList.add('active');
            } else {
                dot.classList.remove('active');
            }
        });
    }

    updateSlider();

    timer = setInterval(() => {
        index = (index + 1) % sliderData.length;
        updateSlider();
    }, 2000);

    rightBtn.addEventListener('click', () => {
        index++;
        if (index >= sliderData.length) {
            index = 0;
        }
        updateSlider();
        clearInterval(timer);
        timer = setInterval(() => {
            index = (index + 1) % sliderData.length;
            updateSlider();
        }, 2000);
    });

    leftBtn.addEventListener('click', () => {
        index--;
        if (index < 0) {
            index = sliderData.length - 1;
        }
        updateSlider();
        clearInterval(timer);
        timer = setInterval(() => {
            index = (index + 1) % sliderData.length;
            updateSlider();
        }, 2000);
    });

    box.addEventListener('mouseenter', () => {
        clearInterval(timer);
    });

    box.addEventListener('mouseleave', () => {
        timer = setInterval(() => {
            index = (index + 1) % sliderData.length;
            updateSlider();
        }, 2000);
    });

    dots.forEach((dot, i) => {
        dot.addEventListener('click', () => {
            index = i;
            updateSlider();
            clearInterval(timer);
            timer = setInterval(() => {
                index = (index + 1) % sliderData.length;
                updateSlider();
            }, 2000);
        });
    });
    </script>
</body>
</html>